﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.SqlClient;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;

namespace PersonLookup.Web
{
    [DataContract]
    public class Person
    {
        [DataMember]
        public int ID { get; set; }
        [DataMember]
        public string FirstName { get; set; }
        [DataMember]
        public string LastName { get; set; }
    }



    [ServiceContract(Namespace = "")]
    [AspNetCompatibilityRequirements(
        RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class PersonService
    {
        private string ConnectionString
        {
            get
            {
                return ConfigurationManager.ConnectionStrings["PersonsDB"].ConnectionString;
            }
        }

        [OperationContract]
        public List<Person> GetPersons()
        {
            var persons = new List<Person>();
            using (var connection = new SqlConnection(ConnectionString))
            {
                connection.Open();
                using (var command = new SqlCommand("select ID, FirstName, LastName from Person", connection))
                {
                    using (var reader = command.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            persons.Add(
                                new Person
                                {
                                    ID = reader.GetInt32(0),
                                    FirstName = reader.GetString(1),
                                    LastName = reader.GetString(2)
                                }
                            );
                        }
                    }
                }
            }
            return persons;
        }

        [OperationContract]
        public Person AddPerson(Person newPerson)
        {
            object result;
            using (var connection = new SqlConnection(ConnectionString))
            {
                connection.Open();
                using (var command = new SqlCommand("insert into Person(FirstName, LastName) values (@firstName, @lastName); select SCOPE_IDENTITY()", connection))
                {
                    command.Parameters.Add("@firstName", System.Data.SqlDbType.NVarChar).Value = newPerson.FirstName;
                    command.Parameters.Add("@lastName", System.Data.SqlDbType.NVarChar).Value = newPerson.LastName;
                    result = command.ExecuteScalar();
                }
            }
            newPerson.ID = Convert.ToInt32((decimal)result);
            return newPerson;
        }

        [OperationContract]
        public int DeletePerson(Person person)
        {
            object result = null;
            using (var connection = new SqlConnection(ConnectionString))
            {
                connection.Open();
                using (var command = new SqlCommand("delete from Person where ID = @id;select @@rowcount", connection))
                {
                    command.Parameters.Add("@id", System.Data.SqlDbType.Int).Value = person.ID;
                    result = command.ExecuteScalar();
                }
            }
            return (int)result;
        }

        [OperationContract]
        public int UpdatePerson(Person person)
        {
            object result;
            using (var connection = new SqlConnection(ConnectionString))
            {
                connection.Open();
                using (var command = new SqlCommand("update Person set FirstName=@firstName, LastName=@lastName where ID=@id; select @@rowcount", connection))
                {
                    command.Parameters.Add("@id", System.Data.SqlDbType.Int).Value = person.ID;
                    command.Parameters.Add("@firstName", System.Data.SqlDbType.NVarChar).Value = person.FirstName;
                    command.Parameters.Add("@lastName", System.Data.SqlDbType.NVarChar).Value = person.LastName;
                    result = command.ExecuteScalar();
                }
            }
            return (int)result;
        }
    }
}
